﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using YouxinHelper.Helper.Currency;

namespace YouxinHelper.Youxin.Local
{
    class Scanner
    {
        internal static void Start(TextBox textBox4, RichTextBox richTextBox1)
        {
            List<string> YouxinFileList = FloderHelper.GetFloderNameList(@"H:\新建文件夹 (2)\Download1");
            int RandomCount = new Random().Next(YouxinFileList.Count);
            string FilePath = YouxinFileList[RandomCount];
            Console.WriteLine(FilePath);
            String Content = "";
            List<string> TempFileList = FileHelper.GetFullFileNameList(FilePath);
            
            int Count = 0;
            if (TempFileList.Count < 5)
            {
                FloderHelper.FolderDelete(FilePath);
                textBox4.Text = "文件数量不足";
                return;
            }
            foreach (string Line in TempFileList) {
                Console.WriteLine(Line);
                Content += string.Format("![]({0})", UploadImg(Line));
                Console.WriteLine("总量{0}，已发布{1}",TempFileList.Count,++Count);
                if (Count > 11)
                    break;
                
            }
            richTextBox1.Text = Content;
            textBox4.Text = Path.GetFileNameWithoutExtension(FilePath).Replace("[手绘]", "");
            FloderHelper.FolderDelete(FilePath);
        }

        private static string UploadImg(string line)
        {
            string Imgurl = "";
            String API = " https://zs.mtkan.cc/upload.php";

            using (WebClient webClient = new WebClient())
            {
                webClient.Headers.Add("Host", new Uri(API).Host);
                webClient.Headers.Add("User-Agent", Helper.StaticValue.MoblieUserAgent);
                webClient.Headers.Add("Accept-Language", "zh-TW,zh;q=0.8,en-US;q=0.5,en;q=0.3");
                webClient.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                webClient.Headers.Add("Accept-Language", "zh-TW,zh;q=0.8,en-US;q=0.5,en;q=0.3");
                webClient.Headers.Add("Pragma", "no-cache");
                webClient.Headers.Add("Cache-Control", "no-cache");

                byte[] responseArray = webClient.UploadFile(API, "POST", line);

                string GetJson = Encoding.GetEncoding("UTF-8").GetString(responseArray);
                Imgurl = AnalysisJson(GetJson);


                Console.WriteLine("发布图{0}", Imgurl);
                return Imgurl;
            }
          
            }

        private static string AnalysisJson(string getJson)
        {
            try
            {
                Root Alljson = JsonConvert.DeserializeObject<Root>(getJson);
                return Alljson.original_pic;
            }
            catch (Exception ex) {
                PrintLog.Log(ex);
                return "";
            }
        }
        public class Root
        {
            /// <summary>
            /// 
            /// </summary>
            public string original_pic { get; set; }
        }
       
    }
}
